package com.csii.adhoc.dataready.serviceImpl;

import com.csii.adhoc.dataready.TDataConstant;
import com.csii.adhoc.dataready.TDataMergeDtlPo;
import com.csii.adhoc.dataready.mapper.TDataMergeDtlMapper;
import com.csii.adhoc.dataready.service.TDataMergeDtlService;
import com.csii.adhoc.exception.ParamValidityException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @description: 数据集接口实现类
 * @author: zyliu
 * @create: 2021-01-13 09:32
 */
@Service("tDataMergeDtlService")
@Slf4j
public class TDataMergeDtlServiceImpl implements TDataMergeDtlService {

    @Autowired
    TDataMergeDtlMapper tDataMergeDtlMapper;

    @Override
    @Transactional
    public void tDataMergeDtlMaintain(TDataMergeDtlPo tDataMergeDtlPo, String optMode) {
        switch (optMode) {
            case TDataConstant.OPT_MODE_UPDATE:
                updateTDataMergeDtlByPrimaryKey(tDataMergeDtlPo);
                break;
            case TDataConstant.OPT_MODE_DELETE:
                deleteByMergeId(tDataMergeDtlPo.getRelationId());
                break;
            default:
                log.error("optMode值不合法:" + optMode);
                throw new ParamValidityException("操作标志不合法:" + optMode);
        }
    }

    @Transactional
    @Override
    public void insertListTDataMergeDtl(List<TDataMergeDtlPo> tdrdps) {
        for(TDataMergeDtlPo tdrdp:tdrdps) {
            if (StringUtils.isEmpty(tdrdp.getRelationId())) {
                tdrdp.setRelationId(UUID.randomUUID().toString().replace("-", ""));
            }
        }
        tDataMergeDtlMapper.insertListTDataMergeDtl(tdrdps);
    }
    @Transactional
    @Override
    public void insertOneTDataMergeDtl(TDataMergeDtlPo tDataMergeDtlPo) {
        if (StringUtils.isEmpty(tDataMergeDtlPo.getRelationId())) {
            tDataMergeDtlPo.setRelationId(UUID.randomUUID().toString().replace("-", ""));
        }
        tDataMergeDtlMapper.insertOneTDataMergeDtl(tDataMergeDtlPo);
    }

    @Transactional
    @Override
    public int updateTDataMergeDtlByPrimaryKey(TDataMergeDtlPo tDataMergeDtlPo) {
        return tDataMergeDtlMapper.updateByPrimaryKey(tDataMergeDtlPo);
    }

    @Transactional
    @Override
    public int deleteByMergeId(String relationId) {
        return tDataMergeDtlMapper.deleteByMergeId(relationId);
    }

    @Transactional
    @Override
    public int deleteTDataMergeDtlByPrimaryKey(String relationId,String masterId,String slaveId) {
        return tDataMergeDtlMapper.deleteByPrimaryKey(relationId,masterId,slaveId);
    }

    @Override
    public List<TDataMergeDtlPo> selectListTDataMergeDtl(String modelId) {
        return tDataMergeDtlMapper.selectListTDataMergeDtl(modelId);
    }

    @Override
    public List<TDataMergeDtlPo> selectListTDataMergeDtlByIds(List list) {
        return tDataMergeDtlMapper.selectListTDataMergeDtlByIds(list);
    }

    @Override
    public List<Map<String,String>> selectTableIdByModelId(String modelId) {
        return tDataMergeDtlMapper.selectTableIdByModelId(modelId);
    }

    @Override
    public TDataMergeDtlPo selectOneTDataMergeDtl(String modelId,String fldEnname) {
        return tDataMergeDtlMapper.selectOneTDataMergeDtl(modelId,fldEnname);
    }
    @Override
    public Map selCustDataDbTable(String sql){
        return tDataMergeDtlMapper.selCustDataDbTable(sql);
    }
}
